package zdh.avatar.base;

//import java.awt.*;
import java.awt.event.*;
//import java.util.*;
import javax.sound.sampled.Clip;
//import javax.swing.*;

/**
 *
 * @author Gem Seeker
 */
public class ButtonHandler implements ActionListener {
    
//    private AvatarApp avatarApp;
    private Clip prevClip;
//    private ElementButton button;
    private SoundClip[] clips;
    private int curClip;
    
    public ButtonHandler(//AvatarApp avatarApp, 
            ElementButton button){
//        this.avatarApp = avatarApp;
//        this.button = button;
        clips = new SoundClip[button.getAudioPaths().length];
        curClip = 0;
        
        // initialize all sound clips
        String[] paths = button.getAudioPaths();
        for(int i=0; i<button.getAudioPaths().length; i++){
            clips[i] = new SoundClip(paths[i]);
        }
    }
    
    @Override
    public void actionPerformed(ActionEvent e){
//        prevClip = avatarApp.getClip(); // get the current playing clip
        if(prevClip != null){           // stop the playing clip
            prevClip.stop();
        }
        
        // play the current clip
        clips[curClip].play();
        // set is as the next clip to play
//        avatarApp.setCurrentClip(clips[curClip++].getClip());
        // wrap around if necessary
        curClip %= clips.length;
        
        // load recommended buttons
//        JPanel recPanel = avatarApp.getRecPanel();
//        FlowLayout layout = new FlowLayout();
//        layout.setAlignment(FlowLayout.LEFT);
//        
//        ArrayList<PrefButton> btns = button.getChildButtons();
//        if(btns != null){
//            for(ElementButton b:btns){
//                JButton btn = new JButton(b.getButtonName());
//                btn.setPreferredSize(new Dimension(150, 30));
//                btn.setBackground(b.getButtonColor());
//                btn.setOpaque(false);
//                btn.addActionListener(new ButtonHandler(avatarApp, b));
//                recPanel.add(btn);
//            }
//        }
//        
//        avatarApp.setRecPanel(recPanel);
    }
    
}// end of ButtonHandler class
